/**
 * @description 全局公用函数
 * @author mgLuoBo
 * @createTime 2019/7/23 0023 22:04
 */
import {requestData} from "./request";
const getUploadImgAction = ()=>{
    return  "/api/ftp/imgupload";
};
const getHeaderImg = ()=>{
    return  "/api/ftp/download/?name=";
};

const getPrefix = ()=>{
    return 'http://i.susonedu.com:8888/ftpupload'
}
/**
 * 保存用户
 * @param user 用户信息
 */
const saveUser = (user) => {
    localStorage.setItem("lessonAdminUser", JSON.stringify(user));
};

/**
 * 移除用户
 */
const removeUser = () => {
    localStorage.removeItem("lessonAdminUser");
};

/**
 * 获取用户
 */
const getUser = () => {
    let user = localStorage.getItem("lessonAdminUser");
    if (isEmpty(user) || user === 'undefined') {
        return {};
    } else {
        return JSON.parse(user);
    }
};

/**
 * 获取当前日期
 * @returns {string} yyyy-MM-dd
 */
const getNowDate = (val,data_) => {
    let date = data_ ? new Date(data_) : new Date();
    if (val !== undefined && val !== 0) {
        let time = data_ ? new Date(data_) : new Date();
        let curTime = 0;
        let startDate = 0;
        if (val > 0) {
            curTime = time.getTime();
            startDate = curTime + (val * 3600 * 24 * 1000);
            date = new Date(startDate);
        } else if (val < 0) {
            curTime = time.getTime();
            startDate = curTime - (Math.abs(val) * 3600 * 24 * 1000);
            date = new Date(startDate);
        }
    }
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    return date.getFullYear() + "-" + month + "-" + strDate;
};
//根据日期  获取星期几
const  getMyDay=(date)=>{
    let week;
    if(date.getDay()===0) week="周日"
    if(date.getDay()===1) week="周一"
    if(date.getDay()===2) week="周二"
    if(date.getDay()===3) week="周三"
    if(date.getDay()===4) week="周四"
    if(date.getDay()===5) week="周五"
    if(date.getDay()===6) week="周六"
    return week;
};


/**
 * 判空
 */
const isEmpty = (obj) => {
    if (typeof obj === "number" && !isNaN(obj)) {
        return false;
    }
    if (!obj) {
        return true;
    }
    return Object.keys(obj).length < 1
};

/**
 * 身份证判断
 */
const isCardId = (card) => {
    let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/;
    return reg.test(card);
};

/**
 * 手机号码判断
 * @param number
 * @returns {boolean}
 */
const isPhoneNumber = (number) => {
    let reg = /^(\+86)?1[3456789]\d{9}$/;
    return reg.test(number);
};

/**
 * 使用@inpute时提供的校验方法 —— 校验纯数字
 * @param value
 * @param isString 是否返回字符串
 * @returns {*}
 */
const checkNumber = (value, isString) => {
    let _value = value.replace(/[^\d]/g, '');
    if(isEmpty(_value) || isString){
        return _value;
    }
    return parseInt(_value);
};

const checkNumberAndWord = (value) => {
    return value.replace(/[\W]/g,'')
};
/**
 * 设置所有校区
 */
const setCampusesList = () => {
    requestData({}, "GET", "/campuses", data => {
        if (data && data.data) {
            localStorage.setItem("campusesList", JSON.stringify(data.data))
        } else {
            localStorage.setItem("campusesList", JSON.stringify([]))
        }
    }, () => {
        localStorage.setItem("campusesList", JSON.stringify([]))
    });
};

/**
 * 设置系统设置
 */
const setVideoSetting = () => {
    requestData({}, "GET", "video/selectVideoConfig", data => {
        if (data && data.data && data.data) {
            localStorage.setItem("videoSetting", JSON.stringify(data.data[0]));
        } else {
            localStorage.setItem("videoSetting", JSON.stringify({}));
        }
    }, () => {
        localStorage.setItem("videoSetting", JSON.stringify({}));
    });
};

/**
 * 获取系统设置
 * @returns {Array} townList
 */
const getVideoSetting = () => {
    let campusesList = localStorage.getItem("videoSetting");
    if (isEmpty(campusesList) || campusesList === 'undefined') {
        return {};
    }
    return JSON.parse(campusesList);
};

/**
 * 获取所有校区
 * @returns {Array} townList
 */
const getCampusesList = () => {
    let campusesList = localStorage.getItem("campusesList");
    if (isEmpty(campusesList) || campusesList === 'undefined') {
        return [];
    }
    return JSON.parse(campusesList);
};


export default {
    getUser,
    saveUser,
    removeUser,
    isEmpty,
    checkNumber,
    getNowDate,
    isPhoneNumber,
    isCardId,
    checkNumberAndWord,
    setCampusesList,
    getCampusesList,
    getUploadImgAction,
    getHeaderImg,
    getPrefix,
    getMyDay,
    setVideoSetting,
    getVideoSetting
}

Date.prototype.format =function(datetime)
{
    var date = new Date(datetime*1);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var year = date.getFullYear(),
        month = ("0" + (date.getMonth() + 1)).slice(-2),
        sdate = ("0" + date.getDate()).slice(-2),
        hour = ("0" + date.getHours()).slice(-2),
        minute = ("0" + date.getMinutes()).slice(-2),
        second = ("0" + date.getSeconds()).slice(-2);
    // 拼接
    var result = year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
    // 返回
    return result;
}
